<?php

class rail_defineApp extends BackendApp
{
    var $_rail_define_mod;
    
    function __construct()
    {
        $this->rail_defineApp();
    }

    function rail_defineApp()
    {
        parent::BackendApp();

        $this->_rail_define_mod =& m('rail_define');

    }

//展示界面
    function index()
    {
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'Name',         //可搜索字段title
                'equal' => 'LIKE',          //等价关系,可以是LIKE, =, <, >, <>
                'name' => 'file_value',         //输入的值
                /*'assoc"=>'AND',   */         //and  或 or

            ),

        ));
        //更新排序
        if (isset($_GET['sort']) && !empty($_GET['order'])) {
            $sort = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order, array('asc', 'desc'))) {
                $sort = 'id';
                $order = 'desc';
            }
        } else {
            if (isset($_GET['sort']) && empty($_GET['order'])) {
                $sort = strtolower(trim($_GET['sort']));
                $order = "";
            } else {
                $sort = 'id';
                $order = 'desc';
            }
        }
        $page = $this->_get_page(10);   //获取分页信息
        $rail_define = $this->_rail_define_mod->find(array(
            'conditions' => '1=1 ' . $conditions,
            'limit' => $page['limit'],
            'count' => true,   //允许统计
            'order' => "$sort $order",
        ));
        $page['item_count'] = $this->_rail_define_mod->getCount();   //获取统计数据
        $this->_format_page($page);
        $this->import_resource(array(
            'script' => 'inline_edit.js',
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'
        ));
        $this->assign('rail_defines', $rail_define);
        $this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
        $this->display('rail_define.index.html');
    }

//添加
    function add()
    {

        if (!IS_POST) {
            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js'
            ));
            $rail_define = array();
            $rail_define['status'] = 1;
            $this->assign('rail_define', $rail_define);
            $this->display('rail_define.form.html');
        } else {
            
            $data = array(
                'name' => $_POST['name'],
                'lat' => $_POST['lat'],
                'lng' => $_POST['lng'],
                'offset' => $_POST['offset'],
                'status' => $_POST['status'],


            );
            if ($this->_rail_define_mod->add($data) === false) {
                $this->show_warning($this->_rail_define_mod->get_error());
                return false;
            }
            $this->show_message('add_ok', '', 'index.php?app=rail_define'
                
            );
        }
    }

//修改
    function edit()
    {
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;

        if (!IS_POST) {

            /* 导入jQuery的表单验证插件 */
            $this->import_resource(array(
                'script' => 'jqtreetable.js,inline_edit.js',
                'style'  => 'res:style/jqtreetable.css'
            ));
            /* 是否存在 */
            $rail_define = $this->_rail_define_mod->get_info($id);
            if (!$rail_define) {
                $this->show_warning('rail_define_empty');
                return;
            }

            $this->assign('rail_define', $rail_define);
            $this->display('rail_define.form.html');
        } else {

            $data = array(
                'name' => $_POST['name'],
                'lat' => $_POST['lat'],
                'lng' => $_POST['lng'],
                'offset' => $_POST['offset'],
                'status' => $_POST['status'],


            );
            $this->_rail_define_mod->edit($id, $data);
            $this->show_message('edit_ok', '', 'index.php?app=rail_define'
                
            );
        }
    }

    /**
     *  删除
     */
    function drop()
    {
        $rail_define_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
        if (!$rail_define_ids) {
            $this->show_warning('no_such_rail_define');

            return;
        }
        $rail_define_ids = explode(',', $rail_define_ids);
        $this->_rail_define_mod->drop($rail_define_ids);
        if ($this->_rail_define_mod->has_error())    //删除
        {
            $this->show_warning($this->_rail_define_mod->get_error());

            return;
        }

        $this->show_message('drop_ok', '', 'index.php?app=rail_define');
    }


//异步修改数据
    function ajax_col()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value = isset($_GET['value']) ? trim($_GET['value']) : '';
        $data = array();

        if (in_array($column, array('status'))) {
            $data[$column] = $value;
            $this->_rail_define_mod->edit($id, $data);
            
            if (!$this->_rail_define_mod->has_error()) {
                echo ecm_json_encode(true);
            }
        } else {
            return ;
        }
        return ;
    }

//上传图片
    function _upload_logo($brand_id)
    {
        $file = $_FILES['Img'];
        if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
        {
            return '';
        }
        import('uploader.lib');             //导入上传类
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES['Img']);//上传logo

        if (!$uploader->file_info()) {
            $this->show_warning($uploader->get_error(), 'go_back', 'index.php?app=rail_define&amp;act=detail_edit&amp;id=' . $brand_id);
            return false;
        }
        // 指定保存位置的根目录
        $uploader->root_dir(ROOT_PATH);
        $imgname = gmtime();
        // 上传
        if ($file_path = $uploader->save('data/files/mall/activity', $imgname))   //保存到指定目录，并以指定文件名$brand_id存储
        {
            /* 生成缩略图 */
            $thumbnail = dirname($file_path) . '/small_' . basename($file_path);
            make_thumb(ROOT_PATH . '/' . $file_path, ROOT_PATH .'/' . $thumbnail, 150, 150, 100);
            return $thumbnail;

        } else {
            return false;
        }
    }



  function map_select()
  {
      $lng=empty($_GET['lng'])?"":$_GET['lng'];
      $lat=empty($_GET['lat'])?"":$_GET['lat'];
      $this->assign('lng', $lng);
      $this->assign('lat', $lat);
      $this->display('rail_define.mapselect.html');
  }
    
}

?>
